/**
 * 
 */
package com.gr.staffpm.pages.schedule;

import java.util.List;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.joda.time.DateTime;

import com.gr.staffpm.pages.schedule.model.Event;


/**
 * @author Graham Rhodes 31 Mar 2011 20:02:12
 */
public class DayEventPanel extends Panel {

    private static final long serialVersionUID = 1L;
    private final RepeatingView events = new RepeatingView("events");

    public DayEventPanel(String id, final DateTime date, List<Event> dayEvents) {
        super(id);

        add(new WebMarkupContainer("bg") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                String cls = "ext-cal-day ext-cal-day-first ";
                cls += isToday(date) ? "ext-cal-day-today" : "";
                tag.getAttributes().put("class", cls);
            }

        });

        add(new WebMarkupContainer("dayTitleContainer") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                String cls = "ext-cal-dtitle  ext-cal-dtitle-first ";
                cls += isToday(date) ? "ext-cal-dtitle-today" : "";
                tag.getAttributes().put("class", cls);
            };

        }.add(new Label("dayTitle", date.toString("EEEEE, MMMMM dd, yyyy"))));

        for (final Event event : dayEvents) {
            WebMarkupContainer panel = new WebMarkupContainer(RandomStringUtils.randomNumeric(10)) {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onComponentTag(ComponentTag tag) {
                    tag.getAttributes().put("class", "ext-cal-ev");
                };

            };
            Label title = new Label("title", event.getTitle()) {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onComponentTag(ComponentTag tag) {
                    String cls = "ext-color-" + event.getCalendar().getId() + (event.isAllDay() ? "-ad " : " ") + " ext-cal-evt ext-cal-evr ";
                    boolean spanLeft = event.startsBefore(date);
                    boolean spanRight = event.endsAfter(date);
                    cls += (spanLeft ? (spanRight ? "ext-cal-ev-spanboth" : "ext-cal-ev-spanleft") : (spanRight ? "ext-cal-ev-spanright" : ""));
                    tag.getAttributes().put("class", cls);
                }

            };
            panel.setVisible(event.isAllDay());
            events.add(panel.add(title));
        }
        add(events);
    }

    protected boolean isToday(DateTime dt) {
        DateTime now = new DateTime();
        return (dt.getDayOfMonth() == now.getDayOfMonth() && dt.getMonthOfYear() == now.getMonthOfYear() && dt.getYear() == now.getYear());
    }

}
